/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.controller;

import com.bayu.common.lib.CommonUtil;
import com.bayu.services.dao.RekapKecelakaanKerjaDao;
import com.bayu.services.entity.RekapKecelakaanKerja;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author Bayu
 */
@Controller
@RequestMapping("/rekap-kecelakaan-kerja-update.htm")
public class RekapKecelakaanKerjaUpdate {
private RekapKecelakaanKerjaDao dao;

    @Autowired
    public void setDao(RekapKecelakaanKerjaDao dao) {
        this.dao = dao;
    }

     @RequestMapping(method=RequestMethod.GET)
    public void displayForm(ModelMap map, String id) {
        List<RekapKecelakaanKerja> data = this.dao.getById(Long.parseLong(id));


        map.addAttribute("lalu5", data.get(0).getLalu5());
        map.addAttribute("lalu4", data.get(0).getLalu4());
        map.addAttribute("lalu3", data.get(0).getLalu3());
        map.addAttribute("lalu2", data.get(0).getLalu2());
        map.addAttribute("lalu1", data.get(0).getLalu1());
    }

    @RequestMapping(method = RequestMethod.POST)
    public String proccessForm(@ModelAttribute("RekapKecelakaanKerja") RekapKecelakaanKerja entity, BindingResult result,ModelMap map, String month,HttpServletRequest req){

          String id = req.getParameter("id");
          /*DateFormat formarter = new SimpleDateFormat("yy-MM-dd");
            Date tgl;

            //exchangeCurrency.setDate(tgl);
            if(result.hasFieldErrors("month")){
                try {
                    tgl = formarter.parse(month);
                    entity.setMonth(tgl);
                } catch (ParseException ex) {
                    Logger.getLogger(KecelakaanKerjaUpdate.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
		*/

          if (id != null) {
             entity.setId(Long.parseLong(id));
             this.dao.update(entity);
             return "redirect:view-rekap-kecelakaan-kerja-data.htm?msg=Data has been updated";
          }else{
             return "redirect:rekap-kecelakaan-kerja-update.htm";
          }

    }
}
